package ModelImplementation

import (
	"SoftwareEngineering/Model"
	"time"
)

type UnionAffiliation struct {
	itsMemberId       int
	itsDues           float64
	itsServiceCharges map[time.Time]ServiceCharge
}

func(u UnionAffiliation) UnionAffiliation(memberId int, dues float64) UnionAffiliation{
	u.itsMemberId = memberId
	u.itsDues = dues
	return u
}

func(u UnionAffiliation) GetServiceCharge(date time.Time) ServiceCharge{
	return u.itsServiceCharges[date]
}

func(u UnionAffiliation) AddServiceCharge(date time.Time, amount float64){
	sc := ServiceCharge{date,amount}
	u.itsServiceCharges[date] = sc 
}

func(u UnionAffiliation) GetDues() float64{
	return u.itsDues
}

func(u UnionAffiliation) GetMemberId() int{
	return u.itsMemberId
}

func(u UnionAffiliation) CalculateDeductions(pc Model.Paycheck) float64{
	totalServiceCharge, totalDues := 0.0, 0.0

	for date, sc := range u.itsServiceCharges {
		dt1 := pc.GetPayPeriodStartDate()
		dt2 := pc.GetPayPeriodEndDate()

		if date.After(dt1) && date.Before(dt2) {
			totalServiceCharge += sc.GetAmount()
		}
	}

	fridays := u.numberOfFridayInPayPeriod(pc.GetPayPeriodStartDate(),
		pc.GetPayPeriodEndDate())
	totalDues = u.itsDues * float64(fridays)
	return totalDues + totalServiceCharge
}

func(u UnionAffiliation) numberOfFridayInPayPeriod(payPeriodStart, payPeriodEnd time.Time) int {
	friday := 0

	for date := payPeriodStart; date.Before(payPeriodEnd); date.AddDate(0, 0, 1) {
		if date.Weekday() == time.Friday {
			friday++
		}
	}
	return friday
}